package com.likai.blog.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.likai.blog.pojo.Comment;
import com.likai.blog.service.IBlogService;
import com.likai.blog.service.ICommentService;
import com.likai.blog.utils.AnswerJson;

@Controller
@RequestMapping(value="/comment")
public class CommentController {

	@Autowired
	@Qualifier("commentService")
	private ICommentService commentService ;
	
	@Autowired
	@Qualifier("blogService")
	private IBlogService blogService ;
	
	/**
	 * 修改评论
	 * @param po
	 * @param request
	 * @param code
	 * @return
	 */
	@RequestMapping(value="/addInfo",method=RequestMethod.POST)
	public ResponseEntity<AnswerJson> addInfo(
			Comment po,
			HttpServletRequest request,
			String imageCode
			) {
		AnswerJson json = new AnswerJson() ;
		String sRand = (String) request.getSession().getAttribute("sRand") ;
		if(sRand.equals(imageCode)) {
			//获取用户Ip
			po.setUserIp(request.getRemoteAddr());
			po.setCommentDate(new Date());
			int result = commentService.updateInfo(po) ;
			if(result == 1) {
				json.setData("修改成功");
			} else {
				json.setData("修改失败");
			}
		} else {
			json.setData("验证码错误");
		}
		
		return new ResponseEntity<AnswerJson>(json,HttpStatus.OK) ;
	}
}
